﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraTab;
using DevExpress.XtraBars.Navigation;
using DevExpress.XtraVerticalGrid;
using LSQL;
using DevExpress.Utils.DirectXPaint;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using DevExpress.XtraVerticalGrid.Native;
using System.Drawing.Text;
using DocumentFormat.OpenXml.Office2016.Presentation.Command;
using System.Web.UI.Design.WebControls;
using DocumentFormat.OpenXml.Vml.Office;

namespace Shopfloormanagement.PDCA
{
    public partial class PDCA : DevExpress.XtraEditors.XtraForm
    {
        public static string pdcaNumber = string.Empty;
        public static PDCA instance = new PDCA();
        public PDCA()
        {
            instance = this;
            InitializeComponent();
            Pinfo_changed = false;
            Dinfo_changed = false;
            Cinfo_changed = false;
            Ainfo_changed = false;

        }

        #region 处理顶部PDCA

        private int P_index = 0;
        private int D_index = 0;
        private int C_index = 0;
        private int A_index = 0;

        private void PictureBoxInit(int i, Image img)
        {
            // 假设P_Index的值是1、2、3等
            string pictureBoxName = $"pic{i}_P"; // 构建控件名称

            // 查找对应的PictureBox
            PictureBox targetPictureBox = this.Controls.Find(pictureBoxName, true).FirstOrDefault() as PictureBox;

            if (targetPictureBox != null)
            {
                // 对找到的PictureBox进行赋值操作
                targetPictureBox.Image = img; // 设置图片
            }
        }

        private void LoadPDCA_Status()
        {
            //数据库查询
            DataTable dt = SQL.ReturnDataTableAsc(SystemInfo.SQL, "PDCA_view", $"ID = {pdcaID}");
            P_index = (int)dt.Rows[0]["P_Status"];
            D_index = (int)dt.Rows[0]["D_Status"];
            C_index = (int)dt.Rows[0]["C_Status"];
            A_index = (int)dt.Rows[0]["A_Status"];

            switch (P_index)
            {
                case 1:
                    pic1_P.Image = Properties.Resources.P完成;
                    pic2_P.Image = Properties.Resources.P完成;
                    pic3_P.Image = Properties.Resources.P完成;
                    pic4_P.Image = Properties.Resources.P完成;
                    break;
                case 2:
                    pic1_P.Image = Properties.Resources.P进行中;
                    pic2_P.Image = Properties.Resources.P进行中;
                    pic3_P.Image = Properties.Resources.P进行中;
                    pic4_P.Image = Properties.Resources.P进行中;
                    break;
                case 3:
                    pic1_P.Image = Properties.Resources.P超期;
                    pic2_P.Image = Properties.Resources.P超期;
                    pic3_P.Image = Properties.Resources.P超期;
                    pic4_P.Image = Properties.Resources.P超期;
                    break;
                case 4:
                    pic1_P.Image = Properties.Resources.P未进行;
                    pic2_P.Image = Properties.Resources.P未进行;
                    pic3_P.Image = Properties.Resources.P未进行;
                    pic4_P.Image = Properties.Resources.P未进行;
                    break;
            }

            switch (D_index)
            {
                case 1:
                    pic1_D.Image = Properties.Resources.D完成;
                    pic2_D.Image = Properties.Resources.D完成;
                    pic3_D.Image = Properties.Resources.D完成;
                    pic4_D.Image = Properties.Resources.D完成;
                    break;
                case 2:
                    pic1_D.Image = Properties.Resources.D进行中;
                    pic2_D.Image = Properties.Resources.D进行中;
                    pic3_D.Image = Properties.Resources.D进行中;
                    pic4_D.Image = Properties.Resources.D进行中;
                    break;
                case 3:
                    pic1_D.Image = Properties.Resources.D超期;
                    pic2_D.Image = Properties.Resources.D超期;
                    pic3_D.Image = Properties.Resources.D超期;
                    pic4_D.Image = Properties.Resources.D超期;
                    break;
                case 4:
                    pic1_D.Image = Properties.Resources.D未进行;
                    pic2_D.Image = Properties.Resources.D未进行;
                    pic3_D.Image = Properties.Resources.D未进行;
                    pic4_D.Image = Properties.Resources.D未进行;
                    break;
            }

            switch (C_index)
            {
                case 1:
                    pic1_C.Image = Properties.Resources.C完成;
                    pic2_C.Image = Properties.Resources.C完成;
                    pic3_C.Image = Properties.Resources.C完成;
                    pic4_C.Image = Properties.Resources.C完成;
                    break;
                case 2:
                    pic1_C.Image = Properties.Resources.C进行中;
                    pic2_C.Image = Properties.Resources.C进行中;
                    pic3_C.Image = Properties.Resources.C进行中;
                    pic4_C.Image = Properties.Resources.C进行中;
                    break;
                case 3:
                    pic1_C.Image = Properties.Resources.C超期;
                    pic2_C.Image = Properties.Resources.C超期;
                    pic3_C.Image = Properties.Resources.C超期;
                    pic4_C.Image = Properties.Resources.C超期;
                    break;
                case 4:
                    pic1_C.Image = Properties.Resources.C未进行;
                    pic2_C.Image = Properties.Resources.C未进行;
                    pic3_C.Image = Properties.Resources.C未进行;
                    pic4_C.Image = Properties.Resources.C未进行;
                    break;
            }

            switch (A_index)
            {
                case 1:
                    pic1_A.Image = Properties.Resources.A完成;
                    pic2_A.Image = Properties.Resources.A完成;
                    pic3_A.Image = Properties.Resources.A完成;
                    pic4_A.Image = Properties.Resources.A完成;
                    break;
                case 2:
                    pic1_A.Image = Properties.Resources.A进行中;
                    pic2_A.Image = Properties.Resources.A进行中;
                    pic3_A.Image = Properties.Resources.A进行中;
                    pic4_A.Image = Properties.Resources.A进行中;
                    break;
                case 3:
                    pic1_A.Image = Properties.Resources.A超期;
                    pic2_A.Image = Properties.Resources.A超期;
                    pic3_A.Image = Properties.Resources.A超期;
                    pic4_A.Image = Properties.Resources.A超期;
                    break;
                case 4:
                    pic1_A.Image = Properties.Resources.A未进行;
                    pic2_A.Image = Properties.Resources.A未进行;
                    pic3_A.Image = Properties.Resources.A未进行;
                    pic4_A.Image = Properties.Resources.A未进行;
                    break;
            }

        }
        #endregion

        #region
        public void AddControl(XtraScrollableControl xtraTabPage)
        {
            xtraTabPage.VerticalScroll.Value = 0;
            int a = xtraTabPage.Controls.Count - 2;//实际数量
            int b = -2;
            int num = 0;
            foreach (Control control in xtraTabPage.Controls)
            {
                if (control.Visible == true)
                {
                    b++;
                }
                num = b + 3;
            }

            Panel panel = new Panel
            {
                Size = new Size(1055, 59),
                Location = new Point(50, ((b + 2) * 90) + 3),
                Name = "plS_" + num,
                Tag = a,
                Visible = true,
            };
            //Console.WriteLine(panel.Name + " Location: " + panel.Location);

            LabelControl label = new LabelControl
            {
                Font = new Font("Tahoma", 12f),
                ForeColor = Color.FromArgb(150, 150, 150),
                Location = new Point(11, 20),
                Text = num.ToString() + "."
            };

            TextEdit textEdit1 = new TextEdit
            {
                Parent = panel,
                Font = new Font("Tahoma", 16),
                Width = 464,
                Location = new Point(66, 10),
                Name = "txtS_Action" + (xtraTabPage.Controls.Count + 1)
            };

            TextEdit textEdit2 = new TextEdit
            {
                Parent = panel,
                Font = new Font("Tahoma", 16),
                Width = 156,
                Location = new Point(559, 10),
                Name = "txtS_Who" + (xtraTabPage.Controls.Count + 1)
            };

            DateEdit dateEdit = new DateEdit
            {
                Parent = panel,
                Font = new Font("Tahoma", 16),
                Width = 156,
                Location = new Point(743, 10),
                Name = "dtpS_When" + (xtraTabPage.Controls.Count + 1)
            };

            ComboBoxEdit comboBoxEdit = new ComboBoxEdit
            {
                Parent = panel,
                Font = new Font("Tahoma", 16),
                Width = 100,
                Location = new Point(938, 10),
                Name = "cbS_Status" + (xtraTabPage.Controls.Count + 1)

            };

            panel.Controls.Add(label);
            panel.Controls.Add(textEdit1);
            panel.Controls.Add(textEdit2);
            panel.Controls.Add(dateEdit);
            panel.Controls.Add(comboBoxEdit);
            publicFunc.InitImgCombobox(comboBoxEdit);

            xtraTabPage.Controls.Add(panel);

        }

        public void AddControl1(XtraScrollableControl xtraTabPage)
        {
            xtraTabPage.VerticalScroll.Value = 0;
            int a = xtraTabPage.Controls.Count - 2;//实际数量
            int b = -2;
            int num = 0;
            foreach (Control control in xtraTabPage.Controls)
            {
                if (control.Visible == true)
                {
                    b++;
                }
                num = b + 3;
            }

            Panel panel = new Panel
            {
                Size = new Size(1055, 59),
                Location = new Point(50, ((b + 2) * 90) + 3),
                Name = "plL_" + num,
                Tag = a,
                Visible = true,
            };
            //Console.WriteLine(panel.Name + " Location: " + panel.Location);

            LabelControl label = new LabelControl
            {
                Font = new Font("Tahoma", 12f),
                ForeColor = Color.FromArgb(150, 150, 150),
                Location = new Point(11, 20),
                Text = num.ToString() + "."
            };

            TextEdit textEdit1 = new TextEdit
            {
                Parent = panel,
                Font = new Font("Tahoma", 16),
                Width = 464,
                Location = new Point(66, 10),
                Name = "txtL_Action" + (xtraTabPage.Controls.Count + 1)
            };

            TextEdit textEdit2 = new TextEdit
            {
                Parent = panel,
                Font = new Font("Tahoma", 16),
                Width = 156,
                Location = new Point(559, 10),
                Name = "txtL_Who" + (xtraTabPage.Controls.Count + 1)
            };

            DateEdit dateEdit = new DateEdit
            {
                Parent = panel,
                Font = new Font("Tahoma", 16),
                Width = 156,
                Location = new Point(743, 10),
                Name = "dtpL_When" + (xtraTabPage.Controls.Count + 1)
            };

            ComboBoxEdit comboBoxEdit = new ComboBoxEdit
            {
                Parent = panel,
                Font = new Font("Tahoma", 16),
                Width = 100,
                Location = new Point(938, 10),
                Name = "cbL_Status" + (xtraTabPage.Controls.Count + 1)

            };

            panel.Controls.Add(label);
            panel.Controls.Add(textEdit1);
            panel.Controls.Add(textEdit2);
            panel.Controls.Add(dateEdit);
            panel.Controls.Add(comboBoxEdit);
            publicFunc.InitImgCombobox(comboBoxEdit);

            xtraTabPage.Controls.Add(panel);

        }

        public void AddControlinAstep(XtraScrollableControl xtraTabPage)
        {
            xtraTabPage.VerticalScroll.Value = 0;
            int a = xtraTabPage.Controls.Count - 1;//实际数量
            int b = -1;
            int num = 0;
            foreach (Control control in xtraTabPage.Controls)
            {
                if (control.Visible == true)
                {
                    b++;
                }
                num = b + 2;
            }

            Panel panel = new Panel
            {
                Size = new Size(1111, 64),
                Location = new Point(3, ((b + 1) * 85) + 14),
                Name = "pl_A" + num,
                Tag = num,
                Visible = true,
            };
            //Console.WriteLine(panel.Name + " Location: " + panel.Location);

            LabelControl label = new LabelControl
            {
                Font = new Font("Tahoma", 12f),
                ForeColor = Color.FromArgb(150, 150, 150),
                Location = new Point(42, 23),
                Text = num.ToString() + "."
            };

            TextEdit textEdit1 = new TextEdit
            {
                Parent = panel,
                Font = new Font("Tahoma", 16),
                Width = 464,
                Location = new Point(97, 12),
                Name = "txtA_Action" + (xtraTabPage.Controls.Count + 1)
            };

            TextEdit textEdit2 = new TextEdit
            {
                Parent = panel,
                Font = new Font("Tahoma", 16),
                Width = 156,
                Location = new Point(590, 12),
                Name = "txtA_Who" + (xtraTabPage.Controls.Count + 1)
            };

            DateEdit dateEdit = new DateEdit
            {
                Parent = panel,
                Font = new Font("Tahoma", 16),
                Width = 156,
                Location = new Point(774, 12),
                Name = "dtpA_When" + (xtraTabPage.Controls.Count + 1)
            };

            ComboBoxEdit comboBoxEdit = new ComboBoxEdit
            {
                Parent = panel,
                Font = new Font("Tahoma", 16),
                Width = 100,
                Location = new Point(969, 12),
                Name = "cbA_Status" + (xtraTabPage.Controls.Count + 1)

            };

            panel.Controls.Add(label);
            panel.Controls.Add(textEdit1);
            panel.Controls.Add(textEdit2);
            panel.Controls.Add(dateEdit);
            panel.Controls.Add(comboBoxEdit);
            publicFunc.InitImgCombobox(comboBoxEdit);

            xtraTabPage.Controls.Add(panel);

        }

        public void AddControlinAction(XtraScrollableControl xtraScrollable)
        {
            xtraScrollable.VerticalScroll.Value = 0;
            int a = xtraScrollable.Controls.Count - 1;//实际数量
            int b = -1;
            int num = 0;
            foreach (Control control in xtraScrollable.Controls)
            {
                if (control.Visible == true)
                {
                    b++;
                }
                num = b + 2;
            }

            Panel panel = new Panel
            {
                Size = new Size(1100, 59),
                Location = new Point(18, (b * 100) + 103),
                Name = "pl_C" + num,
                Tag = a,
                Visible = true,
            };
            //Console.WriteLine(panel.Name + " Location: " + panel.Location);

            LabelControl label = new LabelControl
            {
                Font = new Font("Tahoma", 12f),
                ForeColor = Color.FromArgb(150, 150, 150),
                Location = new Point(11, 21),
                Text = num.ToString() + "."
            };

            TextEdit textEdit1 = new TextEdit
            {
                Parent = panel,
                Font = new Font("Tahoma", 16),
                Width = 464,
                Location = new Point(66, 10),
                Name = "txtAction" + (xtraScrollable.Controls.Count + 1)
            };

            TextEdit textEdit2 = new TextEdit
            {
                Parent = panel,
                Font = new Font("Tahoma", 16),
                Width = 464,
                Location = new Point(619, 10),
                Name = "txtResult" + (xtraScrollable.Controls.Count + 1)
            };



            panel.Controls.Add(label);
            panel.Controls.Add(textEdit1);
            panel.Controls.Add(textEdit2);

            xtraScrollable.Controls.Add(panel);
        }

        private void btnAddAction_Click(object sender, EventArgs e)
        {
            AddControl(plShort);
            plShort.VerticalScroll.Value = plShort.VerticalScroll.Maximum;
        }

        private bool Pinfo_changed;
        private bool Dinfo_changed;
        private bool Cinfo_changed;
        private bool Ainfo_changed;
        private void P_TextChange(object sender, EventArgs e)
        {
            Pinfo_changed = true;
        }

        private void D_TextChange(object sender, EventArgs e)
        {
            Dinfo_changed = true;
        }

        private void C_TextChange(object sender, EventArgs e)
        {
            Cinfo_changed = true;
        }
        private void A_TextChange(object sender, EventArgs e)
        {
            Ainfo_changed = true;
        }
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            AddControl1(plLong);
            plLong.VerticalScroll.Value = plLong.VerticalScroll.Maximum;
        }



        private int lastPageIndex = -1;
        private void navigationFrame1_SelectedPageIndexChanged(object sender, EventArgs e)
        {
            if (navigationFrame1.SelectedPageIndex == lastPageIndex) return;

            lastPageIndex = navigationFrame1.SelectedPageIndex;
            // 你的处理逻辑
            if (navigationFrame1.SelectedPageIndex == 2)
            {
                //DataTable dt = SQL.ReturnDataTableAsc(SystemInfo.SQL, "PDCA_D", $"P_id = {curr_ID}");

                //if (plActions.Controls.Count < dt.Rows.Count)
                //{
                //    for (int i = 0; i < dt.Rows.Count; i++)
                //    {
                //        AddControlinAction(plActions);
                //    }

                //}
                //Console.WriteLine(plActions.Controls.Count.ToString());
                Get_fromDstep();
            }

            if (navigationFrame1.SelectedPageIndex == 1)
            {
                Get_Dinfo();
            }
        }

        private void btnA_add_Click(object sender, EventArgs e)
        {
            AddControlinAstep(pl_Action);
        }

        private void simpleButton6_Click(object sender, EventArgs e)
        {
            navigationFrame1.SelectedPageIndex = 3;
            Load_Ainfo();
        }

        private void simpleButton5_Click(object sender, EventArgs e)
        {
            navigationFrame1.SelectedPageIndex = 1;
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            navigationFrame1.SelectedPageIndex = 0;
        }

        private void btnBack4_Click(object sender, EventArgs e)
        {
            navigationFrame1.SelectedPageIndex = 2;
        }
        #endregion
        public static int curr_ID = 0;
        public static int pdcaID = -1;
        public static bool isNewPDCA = false;
        public static int departmentID;

        private void PDCA_Load(object sender, EventArgs e)
        {
            
            btnP_Next.Visible = false;
            btnP_Save.Visible = true;
            btnD_Next.Visible = false;
            btnC_Next.Visible = false;
            btnClosePDCA.Enabled = true;

            publicFunc.InitImgCombobox(cbS_Status1);
            publicFunc.InitImgCombobox(cbS_Status2);
            publicFunc.InitImgCombobox(cbL_Status1);
            publicFunc.InitImgCombobox(cbL_Status2);
            publicFunc.InitImgCombobox(cbA_Status1);

            if (isNewPDCA == false && curr_ID != 0)
            {
                LoadPDCA_Status();
                Get_Pinfo();
                //Get_Dinfo();
                //Get_fromDstep();
            }
            else
            {
                txtPDCAnumber.Text = pdcaNumber;
                txtPDCAnumber.Enabled = false;
            }
            imgArray.CurrentImageIndex = 0;

        }

        #region P step

        private void Get_Pinfo()
        {
            if (curr_ID == 0)
            {
                XtraMessageBox.Show(LookAndFeel, "未选择PDCA,请返回选择");
                this.Close();
            }
            else
            {
                DataTable dt = SQL.ReturnDataTableAsc(SystemInfo.SQL, "PDCA_P", $"ID = {curr_ID}");
                DataTable dt2 = SQL.ReturnDataTableAsc(SystemInfo.SQL, "PDCA_view", $"ID = {pdcaID}");
                if (dt.Rows.Count > 0) //说明有数据
                {
                    int type = (int)dt2.Rows[0]["ProblemType_id"];
                    string id = pdcaNumber;
                    string descrip = dt.Rows[0]["Description"].ToString();
                    string root = dt.Rows[0]["Rootcause"].ToString();
                    string respon = dt.Rows[0]["Responsible"].ToString();
                    DateTime date = Convert.ToDateTime(dt.Rows[0]["ComplainDate"].ToString());

                    txtPDCAnumber.Text = id;
                    txtDescription.Text = descrip;
                    txtRootcause.Text = root;
                    txtRespon.Text = respon;
                    imgArray.CurrentImageIndex = type - 1;
                    dtpComplain.EditValue = date;
                    if (dt.Rows[0]["ProblemPic"].ToString().Length > 0)
                    {
                        Image img = publicFunc.Base64ToImage(dt.Rows[0]["ProblemPic"].ToString());

                        img_P.Image = img;
                    }


                    btnP_Next.Visible = true;

                }
            }


        }

        private void btnNext_Click(object sender, EventArgs e) //P页的Next按键
        {
            //询问用户是否保存
            navigationFrame1.SelectedPageIndex = 1;
        }

        private void Save_Pinfo()
        {
            string number = txtPDCAnumber.Text;
            string problem = txtDescription.Text;
            string respon = txtRespon.Text;
            string root = txtRootcause.Text;
            DateTime date = dtpComplain.DateTime;

            //if (!publicFunc.GetUserScope(SystemInfo.userID,departmentID) && !publicFunc.GetUserSuper(SystemInfo.userID))
            //{
            //    MessageBox.Show("You have no permisson");
            //    return;
            //}

            if (isNewPDCA)
            {
                //向PDCA list中存储信息
                string column = "DepartmentID,PDCAnumber,ProblemStatement,ProblemType_id,resperson,Create_userID";
                string values = $"{departmentID},'{txtPDCAnumber.Text}','{txtDescription.Text}',{imgArray.CurrentImageIndex + 1},'{txtRespon.Text}','{SystemInfo.userID}'";
                int res = SQL.InsertReturnID(SystemInfo.SQL, "PDCA_view", column, values);
                if (res > 0)
                {
                    string picString = "";
                    if (img_P.Image != null)
                    {
                        picString = publicFunc.ImageToBase64(img_P.Image);
                    }

                    pdcaID = res;
                    string col = $"PDCA_id,Description,Rootcause,Responsible,ComplainDate,ProblemPic";
                    string value = $"'{res}','{problem}','{root}','{respon}','{date.ToString("d")}','{picString}'";
                    int id = SQL.InsertReturnID(SystemInfo.SQL, "PDCA_P", col, value);
                    if (id > 0)
                    {
                        XtraMessageBox.Show(LookAndFeel, "保存成功！");
                        //向PDCA_view中更新状态
                        bool status = SQL.Update(SystemInfo.SQL, "PDCA_view", "P_Status = 1", $"ID = {res}");
                        if (status)
                        {
                            MessageBox.Show("状态更新成功");
                        }

                        curr_ID = id;
                        btnP_Next.Enabled = true;
                        btnP_Next.Visible = true;
                        btnP_Save.Visible = false;

                    }

                }

            }
            else //属于修改已有的PDCA
            {
                string picString = string.Empty;
                if (img_P.Image != null)
                {
                    picString = publicFunc.ImageToBase64(img_P.Image);
                }

                string content = $"Description = '{problem}', Rootcause = '{root}', Responsible = '{respon}', ComplainDate = '{date.ToString("d")}',ProblemPic = '{picString}'";
                bool update = SQL.Update(SystemInfo.SQL, "PDCA_P", content, $"PDCA_id = '{pdcaID}'");
                if (update)
                {
                    XtraMessageBox.Show(LookAndFeel, "保存成功！");

                    //向PDCA_view中更新状态
                    bool status = SQL.Update(SystemInfo.SQL, "PDCA_view", "P_Status = 1", $"ID = {pdcaID}");
                    if (status)
                    {
                        MessageBox.Show("状态更新成功");
                    }
                    btnP_Next.Enabled = true;
                    btnP_Next.Visible = true;
                    btnP_Save.Visible = false;
                }
            }

        }

        private void btnP_Save_Click(object sender, EventArgs e)
        {
            Save_Pinfo();

            LoadPDCA_Status();
        }

        #endregion

        #region D step

        private void Get_Dinfo()
        {
            DataTable dtShort = SQL.ReturnDataTableAsc(SystemInfo.SQL, "PDCA_D", $"P_id = {curr_ID} and ActionType = 'short'"); //short-term action
            DataTable dtLong = SQL.ReturnDataTableAsc(SystemInfo.SQL, "PDCA_D", $"P_id = {curr_ID} and ActionType = 'long'"); //long-term action
            if (dtShort.Rows.Count > 0 || dtLong.Rows.Count > 0)
            {
                btnD_Next.Visible = true;
            }
            if (dtShort.Rows.Count > 0)
            {
                for (int i = 0; i < dtShort.Rows.Count; i++)
                {
                    if (plShort.Controls.Count < dtShort.Rows.Count)
                    {
                        AddControl(plShort); //根据实际数据向short panel中添加空控件
                    }

                    string controlName = "txtS_Action" + (i + 1);
                    Control[] found = this.Controls.Find(controlName, true);
                    if (found.Length > 0 && found[0] is TextEdit txt)
                    {
                        txt.Text = dtShort.Rows[i]["Action"].ToString();
                    }

                    string controlName1 = "txtS_Who" + (i + 1);
                    Control[] found1 = this.Controls.Find(controlName1, true);
                    if (found1.Length > 0 && found1[0] is TextEdit txt1)
                    {
                        txt1.Text = dtShort.Rows[i]["Who"].ToString();
                    }

                    string controlName2 = "dtpS_When" + (i + 1);
                    Control[] found2 = this.Controls.Find(controlName2, true);
                    if (found2.Length > 0 && found2[0] is DateEdit dtp)
                    {
                        dtp.EditValue = Convert.ToDateTime(dtShort.Rows[i]["When"].ToString());
                    }

                    string controlName3 = "cbS_Status" + (i + 1);
                    Control[] found3 = this.Controls.Find(controlName3, true);
                    if (found3.Length > 0 && found3[0] is ComboBoxEdit cb)
                    {
                        cb.SelectedIndex = (int)dtShort.Rows[i]["Status"] - 1;
                    }
                }
            }
            if (dtLong.Rows.Count > 0)
            {
                for (int i = 0; i < dtLong.Rows.Count; i++)
                {
                    if (plLong.Controls.Count < dtLong.Rows.Count)
                    {

                        AddControl1(plLong); //根据实际数据向short panel中添加空控件
                    }

                    string controlName = "txtL_Action" + (i + 1);
                    Control[] found = this.Controls.Find(controlName, true);
                    if (found.Length > 0 && found[0] is TextEdit txt)
                    {
                        txt.Text = dtLong.Rows[i]["Action"].ToString();
                    }

                    string controlName1 = "txtL_Who" + (i + 1);
                    Control[] found1 = this.Controls.Find(controlName1, true);
                    if (found1.Length > 0 && found1[0] is TextEdit txt1)
                    {
                        txt1.Text = dtLong.Rows[i]["Who"].ToString();
                    }

                    string controlName2 = "dtpL_When" + (i + 1);
                    Control[] found2 = this.Controls.Find(controlName2, true);
                    if (found2.Length > 0 && found2[0] is DateEdit dtp)
                    {
                        dtp.EditValue = Convert.ToDateTime(dtLong.Rows[i]["When"].ToString());
                    }

                    string controlName3 = "cbL_Status" + (i + 1);
                    Control[] found3 = this.Controls.Find(controlName3, true);
                    if (found3.Length > 0 && found3[0] is ComboBoxEdit cb)
                    {
                        cb.SelectedIndex = (int)dtLong.Rows[i]["Status"] - 1;
                    }
                }

            }
        }

        private void Save_Dinfo()
        {

            string[] shortInfo = new string[plShort.Controls.Count];
            DateTime[] dateShort = new DateTime[plShort.Controls.Count];
            int[] statusShort = new int[plShort.Controls.Count];

            //收集short信息
            for (int i = 0; i < plShort.Controls.Count; i++)
            {
                string action = "";
                string who = "";
                string when = "";
                string status = "";

                string controlName = "txtS_Action" + (i + 1);
                Control[] found = this.Controls.Find(controlName, true);
                if (found.Length > 0 && found[0] is TextEdit txt)
                {
                    action = txt.Text;
                    if (action.Length < 1)
                    {
                        break;
                    }
                }

                string controlName1 = "txtS_Who" + (i + 1);
                Control[] found1 = this.Controls.Find(controlName1, true);
                if (found1.Length > 0 && found1[0] is TextEdit txt1)
                {
                    who = txt1.Text;
                }

                string controlName2 = "dtpS_When" + (i + 1);
                Control[] found2 = this.Controls.Find(controlName2, true);
                if (found2.Length > 0 && found2[0] is DateEdit dtp)
                {
                    when = dtp.EditValue.ToString();
                    dateShort[i] = Convert.ToDateTime(dtp.EditValue);
                }

                string controlName3 = "cbS_Status" + (i + 1);
                Control[] found3 = this.Controls.Find(controlName3, true);
                if (found3.Length > 0 && found3[0] is ComboBoxEdit cb)
                {
                    status = (cb.SelectedIndex + 1).ToString();
                    statusShort[i] = (cb.SelectedIndex + 1);
                }

                shortInfo[i] = $"Insert into PDCA_D (P_id,ActionType,[Action],Who,[When],Status) values ('{curr_ID}','short','{action}','{who}','{when}',{status});";
            }

            string[] longInfo = new string[plLong.Controls.Count];
            DateTime[] dateLong = new DateTime[plLong.Controls.Count];
            int[] statusLong = new int[plLong.Controls.Count];
            //收集long-term信息
            for (int j = 0; j < plLong.Controls.Count; j++)
            {
                string action = "";
                string who = "";
                string when = "";
                string status = "";

                string controlName = "txtL_Action" + (j + 1);
                Control[] found = this.Controls.Find(controlName, true);
                if (found.Length > 0 && found[0] is TextEdit txt)
                {
                    action = txt.Text;
                    if (action.Length < 1)
                    {
                        break;
                    }
                }

                string controlName1 = "txtL_Who" + (j + 1);
                Control[] found1 = this.Controls.Find(controlName1, true);
                if (found1.Length > 0 && found1[0] is TextEdit txt1)
                {
                    who = txt1.Text;
                }

                string controlName2 = "dtpL_When" + (j + 1);
                Control[] found2 = this.Controls.Find(controlName2, true);
                if (found2.Length > 0 && found2[0] is DateEdit dtp)
                {
                    when = dtp.EditValue.ToString();
                    dateLong[j] = Convert.ToDateTime(dtp.EditValue);

                }

                string controlName3 = "cbL_Status" + (j + 1);
                Control[] found3 = this.Controls.Find(controlName3, true);
                if (found3.Length > 0 && found3[0] is ComboBoxEdit cb)
                {
                    status = (cb.SelectedIndex + 1).ToString();
                    statusLong[j] = (cb.SelectedIndex + 1);
                }

                longInfo[j] = $"insert into PDCA_D (P_id,ActionType,[Action],Who,[When],Status) values ('{curr_ID}','long','{action}','{who}','{when}',{status});";
            }

            string insert1 = string.Join("", shortInfo);
            string insert2 = string.Join("", longInfo);
            string insert = insert1 + insert2;
            int res = SQL.Insert2(SystemInfo.SQL, insert);
            if (res > 0)
            {
                MessageBox.Show($"成功添加{res}条信息。");

                DateTime[] dateArray = dateShort.Concat(dateLong).ToArray();
                int[] statusArray = statusShort.Concat(statusLong).ToArray();

                //更新PDCA_view中的状态
                int status_D = 0;
                for (int i = 0; i < dateArray.Length; i++)
                {
                    if (statusArray[i] == 5)
                    {
                        status_D = 1;
                    }
                    if (statusArray[i] < 5 && statusArray[i] != 0)
                    {
                        status_D = 2;
                    }
                    if (dateArray[i] < DateTime.Now && statusArray[i] != 5 && statusArray[i] != 0)
                    {
                        status_D = 3;
                    }
                }
                bool updateStatus = SQL.Update(SystemInfo.SQL, "PDCA_view", $"D_Status = {status_D}", $"ID = {pdcaID}");
                if (updateStatus)
                {
                    MessageBox.Show("状态保存成功");
                }

                btnD_Next.Visible = true;
            }
        }


        private void simpleButton1_Click_2(object sender, EventArgs e)
        {
            //新增针对第一次录一个，第二次录一堆
            int Sadd = 0;
            for (int idxS = 0; idxS < plShort.Controls.Count; idxS++)
            {
                string controlName = "txtS_Action" + (idxS + 1);
                Control[] found = this.Controls.Find(controlName, true);
                if (found.Length > 0 && found[0] is TextEdit txt)
                {
                    string action = txt.Text;
                    if (action.Length > 1)
                    {
                        Sadd = Sadd + 1;
                    }
                }
            }
        }

        private void Update_Dinfo()
        {
            DataTable dtS = SQL.ReturnDataTableAsc(SystemInfo.SQL, "PDCA_D", $"P_id = {curr_ID} and ActionType = 'short'");
            DataTable dtL = SQL.ReturnDataTableAsc(SystemInfo.SQL, "PDCA_D", $"P_id = {curr_ID} and ActionType = 'long'");

            DateTime[] dateShort = new DateTime[dtS.Rows.Count];
            int[] statusShort = new int[dtS.Rows.Count];

            int[] sArray = new int[dtS.Rows.Count];
            int[] lArray = new int[dtL.Rows.Count];
            for (int idx = 0; idx < dtS.Rows.Count; idx++)
            {
                sArray[idx] = (int)dtS.Rows[idx]["ID"];
            }

            for (int idx = 0; idx < dtL.Rows.Count; idx++)
            {
                lArray[idx] = (int)dtL.Rows[idx]["ID"];
            }

            //string[] shortInfo = new string[plShort.Controls.Count];
            string[] shortInfo = new string[dtS.Rows.Count];
            //收集short信息
            for (int i = 0; i < dtS.Rows.Count; i++)
            {
                string action = "";
                string who = "";
                string when = "";
                string status = "";

                string controlName = "txtS_Action" + (i + 1);
                Control[] found = this.Controls.Find(controlName, true);
                if (found.Length > 0 && found[0] is TextEdit txt)
                {
                    action = txt.Text;
                    if (action.Length < 1)
                    {
                        break;
                    }
                }

                string controlName1 = "txtS_Who" + (i + 1);
                Control[] found1 = this.Controls.Find(controlName1, true);
                if (found1.Length > 0 && found1[0] is TextEdit txt1)
                {
                    who = txt1.Text;
                }

                string controlName2 = "dtpS_When" + (i + 1);
                Control[] found2 = this.Controls.Find(controlName2, true);
                if (found2.Length > 0 && found2[0] is DateEdit dtp)
                {
                    when = dtp.EditValue.ToString();
                    dateShort[i] = Convert.ToDateTime(dtp.EditValue);
                }

                string controlName3 = "cbS_Status" + (i + 1);
                Control[] found3 = this.Controls.Find(controlName3, true);
                if (found3.Length > 0 && found3[0] is ComboBoxEdit cb)
                {
                    status = (cb.SelectedIndex + 1).ToString();
                    statusShort[i] = (cb.SelectedIndex + 1);
                }

                shortInfo[i] = $"Update PDCA_D set [Action] = '{action}',Who = '{who}',[When] = '{when}',Status = {status} where ID = {sArray[i]};";


            }

            string[] longInfo = new string[dtL.Rows.Count];
            DateTime[] dateLong = new DateTime[dtL.Rows.Count];
            int[] statusLong = new int[dtL.Rows.Count];
            //收集long-term信息
            for (int j = 0; j < dtL.Rows.Count; j++)
            {
                string action = "";
                string who = "";
                string when = "";
                string status = "";

                string controlName = "txtL_Action" + (j + 1);
                Control[] found = this.Controls.Find(controlName, true);
                if (found.Length > 0 && found[0] is TextEdit txt)
                {
                    action = txt.Text;
                    if (action.Length < 1)
                    {
                        break;
                    }
                }

                string controlName1 = "txtL_Who" + (j + 1);
                Control[] found1 = this.Controls.Find(controlName1, true);
                if (found1.Length > 0 && found1[0] is TextEdit txt1)
                {
                    who = txt1.Text;
                }

                string controlName2 = "dtpL_When" + (j + 1);
                Control[] found2 = this.Controls.Find(controlName2, true);
                if (found2.Length > 0 && found2[0] is DateEdit dtp)
                {
                    when = dtp.EditValue.ToString();
                    dateLong[j] = Convert.ToDateTime(dtp.EditValue);
                }

                string controlName3 = "cbL_Status" + (j + 1);
                Control[] found3 = this.Controls.Find(controlName3, true);
                if (found3.Length > 0 && found3[0] is ComboBoxEdit cb)
                {
                    status = (cb.SelectedIndex + 1).ToString();
                    statusLong[j] = (cb.SelectedIndex + 1);
                }

                longInfo[j] = $"Update PDCA_D set [Action] = '{action}',Who = '{who}',[When] = '{when}',Status = {status} where ID = {lArray[j]};";
            }

            //新增针对第一次录一个，第二次录一堆
            int Sadd = 0;
            for (int idxS = dtS.Rows.Count; idxS < plShort.Controls.Count; idxS++) //获得现在有的措施总数
            {
                string controlName = "txtS_Action" + (idxS + 1);
                Control[] found = this.Controls.Find(controlName, true);
                if (found.Length > 0 && found[0] is TextEdit txt)
                {
                    string action = txt.Text;
                    if (action.Length > 1)
                    {
                        Sadd = Sadd + 1;
                    }
                }
            }

            int Ladd = 0;
            for (int idxL = dtL.Rows.Count; idxL < plLong.Controls.Count; idxL++)
            {
                string controlName = "txtL_Action" + (idxL + 1);
                Control[] found = this.Controls.Find(controlName, true);
                if (found.Length > 0 && found[0] is TextEdit txt)
                {
                    string action = txt.Text;
                    if (action.Length > 1)
                    {
                        Ladd = Ladd + 1;
                    }
                }
            }

            string[] shortInfoAdd = new string[plShort.Controls.Count];
            DateTime[] dateShortAdd = new DateTime[plShort.Controls.Count];
            int[] statusShortAdd = new int[plShort.Controls.Count];

            //收集short信息
            for (int i = dtS.Rows.Count; i < plShort.Controls.Count; i++)
            {
                string action = "";
                string who = "";
                string when = "";
                string status = "";

                string controlName = "txtS_Action" + (i + 1);
                Control[] found = this.Controls.Find(controlName, true);
                if (found.Length > 0 && found[0] is TextEdit txt)
                {
                    action = txt.Text;
                    if (action.Length < 1)
                    {
                        break;
                    }
                }

                string controlName1 = "txtS_Who" + (i + 1);
                Control[] found1 = this.Controls.Find(controlName1, true);
                if (found1.Length > 0 && found1[0] is TextEdit txt1)
                {
                    who = txt1.Text;
                }

                string controlName2 = "dtpS_When" + (i + 1);
                Control[] found2 = this.Controls.Find(controlName2, true);
                if (found2.Length > 0 && found2[0] is DateEdit dtp)
                {
                    when = dtp.EditValue.ToString();
                    dateShortAdd[i] = Convert.ToDateTime(dtp.EditValue);
                }

                string controlName3 = "cbS_Status" + (i + 1);
                Control[] found3 = this.Controls.Find(controlName3, true);
                if (found3.Length > 0 && found3[0] is ComboBoxEdit cb)
                {
                    status = (cb.SelectedIndex + 1).ToString();
                    statusShortAdd[i] = (cb.SelectedIndex + 1);
                }

                shortInfoAdd[i] = $"Insert into PDCA_D (P_id,ActionType,[Action],Who,[When],Status) values ('{curr_ID}','short','{action}','{who}','{when}',{status});";
            }

            string[] longInfoAdd = new string[plLong.Controls.Count];
            DateTime[] dateLongAdd = new DateTime[plLong.Controls.Count];
            int[] statusLongAdd = new int[plLong.Controls.Count];
            //收集long-term信息
            for (int j = dtL.Rows.Count; j < plLong.Controls.Count; j++)
            {
                string action = "";
                string who = "";
                string when = "";
                string status = "";

                string controlName = "txtL_Action" + (j + 1);
                Control[] found = this.Controls.Find(controlName, true);
                if (found.Length > 0 && found[0] is TextEdit txt)
                {
                    action = txt.Text;
                    if (action.Length < 1)
                    {
                        break;
                    }
                }

                string controlName1 = "txtL_Who" + (j + 1);
                Control[] found1 = this.Controls.Find(controlName1, true);
                if (found1.Length > 0 && found1[0] is TextEdit txt1)
                {
                    who = txt1.Text;
                }

                string controlName2 = "dtpL_When" + (j + 1);
                Control[] found2 = this.Controls.Find(controlName2, true);
                if (found2.Length > 0 && found2[0] is DateEdit dtp)
                {
                    when = dtp.EditValue.ToString();
                    dateLongAdd[j] = Convert.ToDateTime(dtp.EditValue);

                }

                string controlName3 = "cbL_Status" + (j + 1);
                Control[] found3 = this.Controls.Find(controlName3, true);
                if (found3.Length > 0 && found3[0] is ComboBoxEdit cb)
                {
                    status = (cb.SelectedIndex + 1).ToString();
                    statusLongAdd[j] = (cb.SelectedIndex + 1);
                }

                longInfoAdd[j] = $"insert into PDCA_D (P_id,ActionType,[Action],Who,[When],Status) values ('{curr_ID}','long','{action}','{who}','{when}',{status});";
            }

            string insert1 = string.Join("", shortInfo);
            string insert2 = string.Join("", longInfo);
            string insert = insert1 + insert2;
            int res = SQL.Insert2(SystemInfo.SQL, insert);

            string insert3 = string.Join("", shortInfoAdd);
            string insert4 = string.Join("", longInfoAdd);
            string insertAdd = insert3 + insert4;
            int resAdd = SQL.Insert2(SystemInfo.SQL, insertAdd);
            if (res > 0)
            {
                MessageBox.Show($"成功更新{res}条信息。");


                DateTime[] dateArray = dateShort.Concat(dateLong).ToArray();
                DateTime[] dateArrayAdd = dateShortAdd.Concat(dateLongAdd).ToArray();
                int[] statusArray = statusShort.Concat(statusLong).ToArray();
                int[] statusArrayAdd = statusShortAdd.Concat(statusLongAdd).ToArray();

                DateTime[] dateAll = dateArray.Concat(dateArrayAdd).ToArray();
                int[] statusAll = statusArray.Concat(statusArrayAdd).ToArray();

                //更新PDCA_view中的状态
                int status_D = 0;
                for (int i = 0; i < dateAll.Length; i++)
                {
                    if (statusAll[i] == 5)
                    {
                        status_D = 1;
                    }
                    if (statusAll[i] < 5 && statusAll[i] != 0)
                    {
                        status_D = 2;
                    }
                    if (dateAll[i] < DateTime.Now && statusAll[i] != 5 && dateAll[i].ToString() != "0001/1/1 0:00:00")
                    {
                        status_D = 3;
                    }
                    if (status_D > 1)
                    {
                        break;
                    }
                }
                bool updateStatus = SQL.Update(SystemInfo.SQL, "PDCA_view", $"D_Status = {status_D}", $"ID = {pdcaID}");
                if (updateStatus)
                {
                    MessageBox.Show("状态保存成功");
                }

                btnD_Next.Visible = true;
            }
        }

        private void simpleButton1_Click_1(object sender, EventArgs e)
        {
            navigationFrame1.SelectedPageIndex = 2;
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            DataTable dt = SQL.ReturnDataTableAsc(SystemInfo.SQL, "PDCA_D", $"P_id = {curr_ID}");
            if (dt.Rows.Count == 0)
            {
                Save_Dinfo();
                LoadPDCA_Status();
            }
            else
            {
                Update_Dinfo();
                LoadPDCA_Status();
            }

        }
        #endregion

        #region C step

        private void Get_fromDstep()
        {
            DataTable dt = SQL.ReturnDataTableAsc(SystemInfo.SQL, "PDCA_D", $"P_id = {curr_ID}");

            if (dt.Rows.Count > 0)
            {
                if (plActions.Controls.Count < dt.Rows.Count)
                {
                    for (int idx = 1; idx < dt.Rows.Count; idx++)
                    {
                        AddControlinAction(plActions); //根据实际数据向short panel中添加空控件
                    }

                }
                int[] idArray = new int[dt.Rows.Count];
                btnC_Next.Visible = true;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string controlName = "txtAction" + (i + 1);
                    Control[] found = this.Controls.Find(controlName, true);
                    if (found.Length > 0 && found[0] is TextEdit txt)
                    {
                        txt.Text = dt.Rows[i]["Action"].ToString();
                        txt.Tag = (int)dt.Rows[i]["ID"];
                    }
                    idArray[i] = (int)dt.Rows[i]["ID"];
                }

                string ids = string.Join(",", idArray);

                DataTable dtResult = SQL.ReturnDataTable3(SystemInfo.SQL, "PDCA_C", $"Dstep_id in ({ids}) order by Dstep_id");
                if (dtResult.Rows.Count > 0)
                {
                    for (int j = 0; j < dtResult.Rows.Count; j++)
                    {
                        string controlName = "txtResult" + (j + 1);
                        Control[] found = this.Controls.Find(controlName, true);
                        if (found.Length > 0 && found[0] is TextEdit txt)
                        {
                            txt.Text = dtResult.Rows[j]["Result"].ToString();
                        }
                    }
                }

            }

        }

        private void Save_Cinfo()
        {
            DataTable dt = SQL.ReturnDataTableAsc(SystemInfo.SQL, "PDCA_D", $"P_id = {curr_ID}");
            int[] idArray = new int[dt.Rows.Count];
            string[] insert = new string[dt.Rows.Count];
            for (int i = 0; i < plActions.Controls.Count; i++)
            {
                string result = "";
                idArray[i] = (int)dt.Rows[i]["ID"];
                string controlName = "txtResult" + (i + 1);
                Control[] found = this.Controls.Find(controlName, true);
                if (found.Length > 0 && found[0] is TextEdit txt)
                {
                    result = txt.Text;
                }

                insert[i] = $"insert into PDCA_C (Dstep_id,Result) values ({idArray[i]},'{result}');";
            }
            string insertSQL = string.Join("", insert);

            int res = SQL.Insert2(SystemInfo.SQL, insertSQL);
            if (res > 0)
            {
                MessageBox.Show($"成功录入{res}条数据");
                bool updateStatus = SQL.Update(SystemInfo.SQL, "PDCA_view", "C_Status = 1", $"ID = {pdcaID}");
                if (updateStatus)
                {
                    MessageBox.Show("状态更新成功");
                }
                btnC_Next.Visible = true;
            }
        }

        private void Update_Cinfo()
        {
            DataTable dt = SQL.ReturnDataTableAsc(SystemInfo.SQL, "PDCA_C", $"ID in ({Cids})");
            int[] idArray = new int[dt.Rows.Count];
            string[] update = new string[dt.Rows.Count];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string result = "";

                string controlName = "txtResult" + (i + 1);
                Control[] found = this.Controls.Find(controlName, true);
                if (found.Length > 0 && found[0] is TextEdit txt)
                {
                    result = txt.Text;
                }

                update[i] = $"update PDCA_C set Result = '{result}' where ID = {Cid[i]};";
            }
            string insertSQL = string.Join("", update);

            DataTable dt2 = SQL.ReturnDataTableAsc(SystemInfo.SQL, "PDCA_D", $"P_id = {curr_ID}");
            string[] insert2 = new string[plActions.Controls.Count];
            int[] idArray2 = new int[plActions.Controls.Count];
            for (int i = dt.Rows.Count; i < plActions.Controls.Count; i++)
            {
                string result = "";
                idArray2[i] = (int)dt2.Rows[i]["ID"];
                string controlName = "txtResult" + (i + 1);
                Control[] found = this.Controls.Find(controlName, true);
                if (found.Length > 0 && found[0] is TextEdit txt)
                {
                    result = txt.Text;
                }

                insert2[i] = $"insert into PDCA_C (Dstep_id,Result) values ({idArray2[i]},'{result}');";
            }
            string insertSQL2 = string.Join("", insert2);

            int res = SQL.Insert2(SystemInfo.SQL, insertSQL);
            int res2 = SQL.Insert2(SystemInfo.SQL, insertSQL2);
            if (res > 0)
            {
                MessageBox.Show($"成功录入{res + res2}条数据");
                bool updateStatus = SQL.Update(SystemInfo.SQL, "PDCA_view", "C_Status = 1", $"ID = {pdcaID}");
                if (updateStatus)
                {
                    MessageBox.Show("状态更新成功");
                }
                btnC_Next.Visible = true;
            }
        }

        private string Cids = string.Empty;
        private string[] Cid;
        private void btnC_Save_Click(object sender, EventArgs e)
        {
            DataTable dtD = SQL.ReturnDataTableAsc(SystemInfo.SQL, "PDCA_D", $"P_id = {curr_ID}");
            if (dtD.Rows.Count > 0)
            {
                string[] idArray = new string[dtD.Rows.Count];
                for (int i = 0; i < dtD.Rows.Count; i++)
                {
                    idArray[i] = dtD.Rows[i]["ID"].ToString();
                }
                string ids = string.Join(",", idArray);
                DataTable dt = SQL.ReturnDataTableAsc(SystemInfo.SQL, "PDCA_C", $"Dstep_id in ({ids})");
                Cid = new string[dt.Rows.Count];
                for (int idx = 0; idx < dt.Rows.Count; idx++)
                {
                    Cid[idx] = dt.Rows[idx]["ID"].ToString();
                }
                Cids = string.Join(",", Cid);
                if (dt.Rows.Count == 0)
                {
                    Save_Cinfo();
                    LoadPDCA_Status();
                }
                else
                {
                    //进入更新范畴
                    Update_Cinfo();
                    LoadPDCA_Status();
                }
            }


        }


        #endregion

        #region A step
        private void Update_Ainfo()
        {
            string[] update = new string[pl_Action.Controls.Count];

            DateTime[] dateShort = new DateTime[plShort.Controls.Count];
            int[] statusShort = new int[plShort.Controls.Count];
            string names = string.Empty;
            foreach (Control control in pl_Action.Controls)
            {
                 names += $"{control.Name}\n";
            }

            //MessageBox.Show(names);

            DataTable dt = SQL.ReturnDataTableAsc(SystemInfo.SQL, "PDCA_A", $"PDCA_id = {pdcaID}");
            int[] idArray = new int[dt.Rows.Count];
            for (int idx = 0; idx < dt.Rows.Count; idx++)
            {
                idArray[idx] = (int)dt.Rows[idx]["ID"];
            }
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string action = "";
                string who = "";
                string when = "";
                string status = "";

                string controlName = "txtA_Action" + (i + 1);
                Control[] found = this.Controls.Find(controlName, true);
                if (found.Length > 0 && found[0] is TextEdit txt)
                {
                    action = txt.Text;
                }

                string controlName1 = "txtA_Who" + (i + 1);
                Control[] found1 = this.Controls.Find(controlName1, true);
                if (found1.Length > 0 && found1[0] is TextEdit txt1)
                {
                    who = txt1.Text;
                }

                string controlName2 = "dtpA_When" + (i + 1);
                Control[] found2 = this.Controls.Find(controlName2, true);
                if (found2.Length > 0 && found2[0] is DateEdit dtp)
                {
                    when = dtp.EditValue.ToString();
                    dateShort[i] = Convert.ToDateTime(dtp.EditValue);
                }

                string controlName3 = "cbA_Status" + (i + 1);
                Control[] found3 = this.Controls.Find(controlName3, true);
                if (found3.Length > 0 && found3[0] is ComboBoxEdit cb)
                {
                    status = (cb.SelectedIndex + 1).ToString();
                    statusShort[i] = cb.SelectedIndex + 1;
                }


                update[i] = $"update PDCA_A set [Action] = '{action}',[Who] = '{who}',[When] = '{when}',[Status] = '{status}' where ID = {idArray[i]}";
            }


            for (int idx = dt.Rows.Count; idx < pl_Action.Controls.Count; idx++)
            {
                string action = "";
                string who = "";
                string when = "";
                string status = "";

                string controlName = "txtA_Action" + (idx + 1);
                Control[] found = this.Controls.Find(controlName, true);
                if (found.Length > 0 && found[0] is TextEdit txt)
                {
                    action = txt.Text;
                }

                string controlName1 = "txtA_Who" + (idx + 1);
                Control[] found1 = this.Controls.Find(controlName1, true);
                if (found1.Length > 0 && found1[0] is TextEdit txt1)
                {
                    who = txt1.Text;
                }

                string controlName2 = "dtpA_When" + (idx + 1);
                Control[] found2 = this.Controls.Find(controlName2, true);
                if (found2.Length > 0 && found2[0] is DateEdit dtp)
                {
                    when = dtp.EditValue.ToString();
                    dateShort[idx] = Convert.ToDateTime(dtp.EditValue);
                }

                string controlName3 = "cbA_Status" + (idx + 1);
                Control[] found3 = this.Controls.Find(controlName3, true);
                if (found3.Length > 0 && found3[0] is ComboBoxEdit cb)
                {
                    status = (cb.SelectedIndex + 1).ToString();
                    statusShort[idx] = cb.SelectedIndex + 1;
                }

                update[idx] = $"insert into PDCA_A (PDCA_id,[Action],[Who],[When],[Status]) values ({pdcaID},'{action}','{who}','{when}',{status})";
            }

            string up = string.Join(";", update);
            int res = SQL.Insert2(SystemInfo.SQL, up);
            if (res > 0)
            {
                MessageBox.Show($"成功添加{res}条信息");
                //更新PDCA_view中的状态
                int status_D = 0;
                for (int i = 0; i < dateShort.Length; i++)
                {
                    if (statusShort[i] == 5)
                    {
                        status_D = 1;
                    }
                    if (statusShort[i] < 5 && statusShort[i] != 0)
                    {
                        status_D = 2;
                    }
                    if (dateShort[i] < DateTime.Now && statusShort[i] != 5 && statusShort[i] != 0)
                    {
                        status_D = 3;
                    }
                    if (status_D > 1)
                    {
                        break;
                    }

                }
                bool updateStatus = SQL.Update(SystemInfo.SQL, "PDCA_view", $"A_status = {status_D}", $"ID = {pdcaID}");
                if (updateStatus)
                {
                    MessageBox.Show("状态更新成功");
                }
            }
        }
        private void Save_Ainfo()
        {
            string[] inserts = new string[pl_Action.Controls.Count];

            DateTime[] dateShort = new DateTime[plShort.Controls.Count];
            int[] statusShort = new int[plShort.Controls.Count];
            for (int i = 0; i < pl_Action.Controls.Count; i++)
            {
                string action = "";
                string who = "";
                string when = "";
                string status = "";

                string controlName = "txtA_Action" + (i + 1);
                Control[] found = this.Controls.Find(controlName, true);
                if (found.Length > 0 && found[0] is TextEdit txt)
                {
                    action = txt.Text;
                }

                string controlName1 = "txtA_Who" + (i + 1);
                Control[] found1 = this.Controls.Find(controlName1, true);
                if (found1.Length > 0 && found1[0] is TextEdit txt1)
                {
                    who = txt1.Text;
                }

                string controlName2 = "dtpA_When" + (i + 1);
                Control[] found2 = this.Controls.Find(controlName2, true);
                if (found2.Length > 0 && found2[0] is DateEdit dtp)
                {
                    when = dtp.EditValue.ToString();
                    dateShort[i] = Convert.ToDateTime(dtp.EditValue);
                }

                string controlName3 = "cbA_Status" + (i + 1);
                Control[] found3 = this.Controls.Find(controlName3, true);
                if (found3.Length > 0 && found3[0] is ComboBoxEdit cb)
                {
                    status = (cb.SelectedIndex + 1).ToString();
                    statusShort[i] = (cb.SelectedIndex + 1);
                }

                inserts[i] = $"insert into PDCA_A (PDCA_id,[Action],[Who],[When],[Status]) values ({pdcaID},'{action}','{who}','{when}',{status})";
            }

            string insert = string.Join(";", inserts);
            int res = SQL.Insert2(SystemInfo.SQL, insert);
            if (res > 0)
            {
                MessageBox.Show($"成功添加{res}条信息");
                //更新PDCA_view中的状态
                int status_D = 0;
                for (int i = 0; i < dateShort.Length; i++)
                {
                    if (statusShort[i] == 5)
                    {
                        status_D = 1;
                    }
                    if (statusShort[i] < 5)
                    {
                        status_D = 2;
                    }
                    if (dateShort[i] < DateTime.Now && statusShort[i] != 5)
                    {
                        status_D = 3;
                    }
                    if (status_D > 1)
                    {
                        break;
                    }

                }
                bool updateStatus = SQL.Update(SystemInfo.SQL, "PDCA_view", $"A_status = {status_D}", $"ID = {pdcaID}");
                if (updateStatus)
                {
                    MessageBox.Show("状态更新成功");
                }
            }
        }

        private void btnA_Save_Click(object sender, EventArgs e)
        {
            DataTable dt = SQL.ReturnDataTableAsc(SystemInfo.SQL, "PDCA_A", $"PDCA_id = {pdcaID}");
            if (dt.Rows.Count == 0)
            {
                Save_Ainfo();
                LoadPDCA_Status();
            }//LMY
            else
            {
                Update_Ainfo();
                LoadPDCA_Status();
            }
        }


        private void Load_Ainfo()
        {
            DataTable dt = SQL.ReturnDataTableAsc(SystemInfo.SQL, "PDCA_A", $"PDCA_id = {pdcaID}");
            if (pl_Action.Controls.Count < dt.Rows.Count)
            {
                for (int i = 1; i < dt.Rows.Count; i++)
                {
                    AddControlinAstep(pl_Action);
                }
            }
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (plShort.Controls.Count < dt.Rows.Count)
                    {
                        AddControl(plShort); //根据实际数据向short panel中添加空控件
                    }

                    string controlName = "txtA_Action" + (i + 1);
                    Control[] found = this.Controls.Find(controlName, true);
                    if (found.Length > 0 && found[0] is TextEdit txt)
                    {
                        txt.Text = dt.Rows[i]["Action"].ToString();
                    }

                    string controlName1 = "txtA_Who" + (i + 1);
                    Control[] found1 = this.Controls.Find(controlName1, true);
                    if (found1.Length > 0 && found1[0] is TextEdit txt1)
                    {
                        txt1.Text = dt.Rows[i]["Who"].ToString();
                    }

                    string controlName2 = "dtpA_When" + (i + 1);
                    Control[] found2 = this.Controls.Find(controlName2, true);
                    if (found2.Length > 0 && found2[0] is DateEdit dtp)
                    {
                        dtp.EditValue = Convert.ToDateTime(dt.Rows[i]["When"].ToString());
                    }

                    string controlName3 = "cbA_Status" + (i + 1);
                    Control[] found3 = this.Controls.Find(controlName3, true);
                    if (found3.Length > 0 && found3[0] is ComboBoxEdit cb)
                    {
                        cb.SelectedIndex = (int)dt.Rows[i]["Status"] - 1;
                    }
                }
            }

        }


        #endregion
        
        private void simpleButton7_Click_1(object sender, EventArgs e)
        {
            string name = string.Empty;
            Control[] found = this.Controls.Find("pl_A2", true);
            if (found.Length > 0 && found[0] is Panel panel)
            {
                foreach (Control col in found[0].Controls)
                {
                    name += $"{col.Name}; \n";
                }

            }

            MessageBox.Show(name);
        }

        private void btnClosePDCA_Click(object sender, EventArgs e)
        {
            
            if (!publicFunc.GetUserScope(SystemInfo.userID, SystemInfo.readDepartment) && !publicFunc.GetUserSuper(SystemInfo.userID))
            {
                MessageBox.Show("You have no permisson");
                return;
            }
            DialogResult dialog = MessageBox.Show("是否关闭PDCA？", "是否关闭…", MessageBoxButtons.OKCancel);
            if (dialog == DialogResult.OK)
            {
                bool update = SQL.Update(SystemInfo.SQL, "PDCA_view", "isComplete = 1", $"ID = {pdcaID}");
                if (update)
                {
                    MessageBox.Show("成功关闭");
                }
            }

        }

       
    }
}